using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


/*LOOK INTO BUFFER SWAPPING PROPERLLY*/

namespace vengeful_bean
{
    /// <summary>
    /// This is a game manager. This game manager will take control of the vengeful bean, and all of its controllers.
    /// </summary>
    public class GameManager : Microsoft.Xna.Framework.Game
    {
        protected enum INTROSTATE
        {
            INTROMOVIE, MENU, PLAYING, BETWEENLEVEL, OUTRO
        }


        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;
        StringBuilder sb = new StringBuilder();

        LevelManager lvlManager;
        INTROSTATE gameState = INTROSTATE.INTROMOVIE;


        //test
        IntroMovie iMovie = new IntroMovie();
        IntroMenu iMenu = new IntroMenu();
        character vb;

        long timeElapsed = 0;

        public GameManager()
        {
            // TODO: Construct any child components here
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }


        /// <summary>
        /// Load content related to the game (texture, images, font, audio).
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            font = Content.Load<SpriteFont>(@"fonts\SpriteFont1");
            //All managers should start loading content AFTER this point

            lvlManager = new LevelManager(graphics, Content, LEVEL.ONE);
            

            ///testing
            vb = new character(Content);





        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);

            ////All managers should update below this line
            timeElapsed += gameTime.ElapsedGameTime.Milliseconds;

            KeyboardState keyState = Keyboard.GetState();
            //dummy switch
            switch(gameState)
            {
                
                case INTROSTATE.INTROMOVIE:

                    if (iMovie.position.X >= 400 ||keyState.IsKeyDown(Keys.Escape))
                        gameState = INTROSTATE.MENU;

                    if (timeElapsed > 33)
                    {
                        iMovie.position.X += 2;
                        iMovie.position.Y += 2;
                        timeElapsed = 0;
                    }


                    break;

                case INTROSTATE.MENU:

                    iMenu.Update(gameTime);

                    if (iMenu.accepted)
                    {
                        if (iMenu.type == "Exit")
                            Exit();

                        gameState = INTROSTATE.PLAYING;
                        lvlManager = new LevelManager(graphics, Content, iMenu.level);

                    }
                    //Debug.WriteLine("MENU MENU MENU MENU MENU MENU");
                    break;

                case INTROSTATE.PLAYING:
                    
                    //Debug.WriteLine(lvlManager.gameLevel);
                    
                    

                    //lvlManager.Update(gameTime);
                    break;

                case INTROSTATE.BETWEENLEVEL:

                    break;

                case INTROSTATE.OUTRO:

                    break;


            }

            

        }

        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            ////All managers should draw after this line
            GraphicsDevice.Clear(Color.Wheat);
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            //dummy switch
            switch (gameState)
            {
                case INTROSTATE.INTROMOVIE:


                    iMovie.Draw(spriteBatch, font, gameTime);


                    break;

                case INTROSTATE.MENU:

                    iMenu.Draw(spriteBatch, font, gameTime);
                    break;

                case INTROSTATE.PLAYING:

                    vb.draw(gameTime, spriteBatch);
                    
                    //lvlManager.Draw(spriteBatch, gameTime);
                    break;

                case INTROSTATE.BETWEENLEVEL:

                    break;

                case INTROSTATE.OUTRO:

                    break;


            }

           

            spriteBatch.End();

        }

    }
}
